/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package com.bullionxpress.shared.model;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import com.bullionxpress.shared.service.InsufficientFundingException;

/**
 *
 * @author BullionXpress.com
 */
public class Account implements Serializable {

    Vector positions = new Vector();

    String depositAddress = "";
    //private Position position = new Position();
    private String id = "default";
    private String owner = "";
    private double balance;
    private double initial;
    private String name;
    private AccountStatus status = AccountStatus.ACTIVE;

    public Account()
    {

    }

    public String toString()
    {
        return "{" + id + " " + balance + " " + name + "}";
    }
    

    public Position[] getPositions()
    {
        Position[] _positions = new Position[positions.size()];
        Iterator i = positions.iterator();
        int index = 0;
        while( i.hasNext() ) {
            Position p = (Position)i.next();
            _positions[index++] = p;
        }
        return _positions;
    }

    public void setPositions(Position[] list)
    {
        if( list != null ){
            List<Position> t = Arrays.asList(list);
            positions.addAll(t);
        }
    }

    /**
     * @return the balance
     */
    public double getBalance() {
        return balance;
    }

    public void withdraw( double amount ) throws InsufficientFundingException
    {
        if( amount < 0 ){
            throw new IllegalArgumentException("Amount cannot be negative");
        }
        if( balance - amount < 0 ) {
            throw new InsufficientFundingException("Insufficient Funds");
        }
        balance -= amount;
        //dispatch(new AccountEvent());
    }

    public void deposit( double amount )
    {
        if( amount < 0 ){
            throw new IllegalArgumentException("Amount cannot be negative");
        }
        balance += amount;
        //dispatch(new AccountEvent());
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
        //dispatch(new AccountEvent());
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
        //dispatch(new AccountEvent());
    }

    public void print()
    {
        DecimalFormat format = new DecimalFormat("$#,###.00");
        System.out.println("Account:" + id);
        System.out.println("--------------------------------");
        System.out.println("balance:" + format.format(balance));
        //System.out.println("units:" + position.getUnits() );
        System.out.println("owner:" + owner );
        //System.out.println("price:" + position.getEntryPrice() );
        //System.out.println("position:" + position.getInstrument() );

    }

    /**
     * @return the owner
     */
    public String getOwner() {
        return owner;
    }

    /**
     * @param owner the owner to set
     */
    public void setOwner(String owner) {
        this.owner = owner;
    }

    /**
     * @return the initial
     */
    public double getInitial() {
        return initial;
    }

    /**
     * @param initial the initial to set
     */
    public void setInitial(double initial) {
        this.initial = initial;
    }
    
    /**
     * @return the status
     */
    public AccountStatus getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(AccountStatus status) {
        this.status = status;
    }    
    
/**
     * @return the depositAddress
     */
    public String getDepositAddress() {
        return depositAddress;
    }

    /**
     * @param depositAddress the depositAddress to set
     */
    public void setDepositAddress(String depositAddress) {
        this.depositAddress = depositAddress;
    }    

}

